package com.lastproject.ejb.dao;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.lastproject.dto.QuestionDTO;
import com.lastproject.dto.SurveyDTO;
import com.lastproject.ejb.entities.Question;
import com.lastproject.ejb.entities.Survey;
import com.lastproject.ejb.util.QuestionConversion;
import com.lastproject.ejb.util.SurveyConversion;

/**
 * Session Bean implementation class QuestionDAO
 * 
 * Author: Alina
 */
@Stateless
public class QuestionDAO implements QuestionDAORemote {

	/**
	 * Default constructor.
	 */

	@PersistenceContext
	private EntityManager em;
	@EJB
	QuestionConversion conv;

	@EJB
	SurveyConversion sconv;

	public QuestionDAO() {

		super();
	}

	@Override
	public void create(QuestionDTO entity) {
		em.persist(conv.fromDTO(entity));

	}

	@Override
	public void edit(QuestionDTO entity) {
		Question question = em.find(Question.class, conv.fromDTO(entity)
				.getId());
		em.merge(question);

	}

	@Override
	public void remove(QuestionDTO entity) {
		Question question = em.find(Question.class, conv.fromDTO(entity)
				.getId());
		em.remove(question);

	}

	@Override
	public QuestionDTO findById(int id) {
		Question question = em.find(Question.class, id);
		return conv.fromEntity(question);
	}

	@Override
	public List<QuestionDTO> findAll() {
		List<Question> questions = em.createQuery("SELECT q FROM Question q",
				Question.class).getResultList();

		List<QuestionDTO> questionDTO = new ArrayList<QuestionDTO>();

		for (Question question : questions) {
			QuestionDTO q = new QuestionDTO();
			q = conv.fromEntity(question);
			questionDTO.add(q);

		}

		return questionDTO;
	}

	@Override
	public List<QuestionDTO> getQuestionListBySurveyId(int id) {
		List<Question> questions = em
				.createQuery("SELECT q FROM Question q WHERE q.survey.id =:id",
						Question.class).setParameter("id", id).getResultList();

		List<QuestionDTO> questionsListBySurveyId = new ArrayList<QuestionDTO>();

		for (Question question : questions) {
			QuestionDTO q = new QuestionDTO();
			q = conv.fromEntity(question);
			questionsListBySurveyId.add(q);

		}
		return questionsListBySurveyId;
	}

	@Override
	public QuestionDTO findLastByText(String question) {
		List<Question> questions = em
				.createNamedQuery("Question.findLastByText", Question.class)
				.setParameter("text", question).getResultList();
		if (questions.size() == 0) {
			return null;
		}
		return conv.fromEntity(questions.get(questions.size() - 1));
	}

	@Override
	public Object findLastByTextAndSurvey(String question, SurveyDTO surveyDTO) {
		Survey survey = sconv.fromDTO(surveyDTO);
		List<Question> questions = em
				.createNamedQuery("Question.findLastByTextAndSurvey",
						Question.class).setParameter("text", question)
				.setParameter("survey", survey).getResultList();
		if (questions.size() == 0) {
			return null;
		}
		return conv.fromEntity(questions.get(questions.size() - 1));
	}

}
